package session;

import entity.IndicativeProductPrice;
import entity.SalesOrder;
import entity.SalesOrderItem;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author K_M
 */
@Stateful
@LocalBean
public class SalesOrderManager {

    @PersistenceContext
    private EntityManager em;
    private SalesOrder salesOrder;
    private SalesOrderItem salesOrderItem;
    private Collection<SalesOrderItem> salesOrderItemList=new ArrayList<SalesOrderItem>();
    private Collection<SalesOrder> salesOrderList = new ArrayList<SalesOrder>();
    private Collection<IndicativeProductPrice> productList = new ArrayList<IndicativeProductPrice>();

    public void createSalesOrder(Long purchaseOrderId, Integer confirmedLeadTime) //NOT FULL CREATION; USE OF DUMMY DATA
    {
        salesOrder = new SalesOrder();
        salesOrder.setConfirmedLeadTime(confirmedLeadTime);
        salesOrder.setPurchaseOrderId(purchaseOrderId);
        salesOrder.setStatus("Awaiting Processing");
        salesOrder.setLastModifiedBy("SystemUser");
        Date today = new java.util.Date();
        Timestamp dateOrdered = new Timestamp(today.getTime());
        salesOrder.setDateOrdered(dateOrdered);
    }
    public void createSalesOrder(Long purchaseOrderId) //Alternate method for creation(used by convert of purchase)?
    {
        salesOrder = new SalesOrder();
        salesOrder.setPurchaseOrderId(purchaseOrderId);
        salesOrder.setStatus("Awaiting Confirmed Lead Time input");
        salesOrder.setLastModifiedBy("SystemUser");
        Date today = new java.util.Date();
        Timestamp dateOrdered = new Timestamp(today.getTime());
        salesOrder.setDateOrdered(dateOrdered);
    }
/* OLD VERSION TO DELETE IF NOT IN USE
    public void addSalesOrderItem(String productName, Integer numOfProduct,
            Double productPrice, String shippingAddress) {
        salesOrderItem = new SalesOrderItem();
        salesOrderItem.setProductName(productName);
        salesOrderItem.setNumOfProduct(numOfProduct);
        salesOrderItem.setProductPrice(productPrice);
        salesOrderItem.setShippingAddress(shippingAddress);
        salesOrder.getSalesOrderItemList().add(salesOrderItem);
    }
     * */
    
           public void addSalesOrderItem(Collection<SalesOrderItem> salesOrderItems) {
        salesOrderItemList = new ArrayList<SalesOrderItem>();
        for (Object o : salesOrderItems) {
            SalesOrderItem temp = (SalesOrderItem) o;
            salesOrderItemList.add(temp);
        }
        
        salesOrder.setSalesOrderItemList(salesOrderItemList);
    }
           
     public Double calculateTotalPrice(Long salesOrderId) {
        SalesOrder s = getSalesOrder(salesOrderId);
        Double tempTotal = 0.0;
        for (Object o : s.getSalesOrderItemList()) { //loop through salesOrderItemList
            SalesOrderItem temp = (SalesOrderItem) o;
            tempTotal += temp.getProductPrice() * temp.getNumOfProduct();
        }
        return (Math.round(tempTotal * 100)) / 100.0;
    }

    public Double calculateTotalPrice() {

        Double tempTotal = 0.0;
        for (Object o : salesOrder.getSalesOrderItemList()) {
            SalesOrderItem temp = (SalesOrderItem) o;
            tempTotal += temp.getProductPrice() * temp.getNumOfProduct();
        }
        return (Math.round(tempTotal * 100)) / 100.0;
    }

    public void persist() {
        Double temp = calculateTotalPrice();
        temp = (Math.round(temp * 100)) / 100.0;
        salesOrder.setTotalPrice(temp);
        em.flush();
        salesOrder.setTotalPrice(temp);
        em.persist(getSalesOrder());
        em.flush();
    }

    public SalesOrder getSalesOrder() {
        return salesOrder;
    }

    public SalesOrder getSalesOrder(Long id) {
        SalesOrder s;
        s = em.find(SalesOrder.class, id);
        return s;
    }

    public void updateSalesOrderStatus(Long id, String status) {
        SalesOrder o = em.find(SalesOrder.class, id);
        o.setStatus(status);
        em.flush();
    }

    public void updateSalesOrderLastModifiedBy(Long id, String usr) {
        SalesOrder o = em.find(SalesOrder.class, id);
        o.setLastModifiedBy(usr);
        em.flush();
    }

    public void deleteSalesOrder(Long id) {
        SalesOrder o = em.find(SalesOrder.class, id);
        em.remove(o);
        em.flush();
    }

    public void merge(SalesOrder s) {
        em.merge(s);
        em.flush();
    }

    public void setSalesOrderList(Collection<SalesOrder> salesOrderList) {
        this.salesOrderList = salesOrderList;
    }

    public Collection<SalesOrder> getSalesOrderList() {
        return salesOrderList;
    }
        public Collection<SalesOrder> getSalesOrderList(String clientUsername) {
        Query q = em.createQuery("Select i From SalesOrder i where i.lastModifiedBy=:clientUsername");
        q.setParameter("clientUsername", clientUsername);
        for (Object o : q.getResultList()) {
            SalesOrder temp = (SalesOrder) o;
            salesOrderList.add(temp);
        }
        return salesOrderList;
    }
          
    /**
     * @param salesOrder the salesOrder to set
     */
    public void setSalesOrder(SalesOrder salesOrder) {
        this.salesOrder = salesOrder;
    }

    /**
     * @param salesOrderItem the salesOrderItem to set
     */
    public void setSalesOrderItem(SalesOrderItem salesOrderItem) {
        this.salesOrderItem = salesOrderItem;
    }

    /**
     * @param productList the productList to set
     */
    public void setProductList(Collection<IndicativeProductPrice> productList) {
        this.productList = productList;
    }

    public Collection<IndicativeProductPrice> getProductList() {
        productList = new ArrayList<IndicativeProductPrice>();
        Query q = em.createQuery("Select i From Product i");
        for (Object o : q.getResultList()) {
            IndicativeProductPrice temp = (IndicativeProductPrice) o;
            productList.add(temp);
        }
        return productList;
    }
    
        public Collection<SalesOrderItem> getSalesOrderItemList(Long salesOrderId) { //returns a list of purchase order items
        SalesOrder p=em.find(SalesOrder.class, salesOrderId) ;
        return p.getSalesOrderItemList();
        }

    public SalesOrderItem getSalesOrderItem() {
        return salesOrderItem;
    }

    public Collection<SalesOrderItem> getSalesOrderItemList() {
        return salesOrderItemList;
    }

    public void setSalesOrderItemList(Collection<SalesOrderItem> salesOrderItemList) {
        this.salesOrderItemList = salesOrderItemList;
    }
}
